<template>
	<!--地区选择 五级-->
		<div class="diqu" style="width: 100%;height: 40px;overflow: hidden;">
			<diquZu v-for="(r,index) in nr" :key="index" :id="r.id" :value = "r.value" :span="span" :jibie="index" @test="xuan" :areaObj="areas"></diquZu>
		</div>
</template>

<script>
  import diquZu from './liandong_zu.vue'
	export default {
		name: 'diqu',
    components: {
      diquZu
    },
    props: ['filed','list','code','areas'],
		data() {
			return {
				isshow:1,
				span:100,
				nr:[],
        neirong:{},
        fileNmae:'',
        zhi:{},
        shenheform:{},
        itemCode:this.code
			}
		},
		mounted() {
      if(this.filed){
        this.fuzhi(this.filed)
      }
      if(this.list){
        this.shenheform = JSON.parse(this.list)
      }
		},
     watch: {
      'filed': function(n, o) {
        this.fuzhi(n)
      },
      'list':function(n,o){
        this.shenheform = JSON.parse(n)
      }
    },
		methods: {
      fuzhi(val){
        this.ziduan=JSON.parse(val);
        this.value = this.ziduan.value?this.ziduan.value:'';
        this.fileNmae = this.ziduan.fieldName?this.ziduan.fieldName:'';
        this.fieldSrcType = this.ziduan.fieldSrcType?this.ziduan.fieldSrcType:''
        this.fieldSrc = this.ziduan.fieldSrc?this.ziduan.fieldSrc:'';
        /*if(this.fieldSrcType == '10'){//机构
          var obj = this.fieldSrc.replace(/#_itemCode#/g, this.itemCode)
          this.fieldSrc = 'pids=0'
          for(const i in this.shenheform){
            if(i == obj){
              this.fieldSrc = 'pids='+this.shenheform[i]
            }
          }
        }*/
        if(this.value){
          this.zhi = this.ziduan
          if(this.zhi.share){
              this.nr=[]
              this.xiangshang(this.zhi.value)
          }
        }else{
            this.getdiqu(this.fieldSrc);
        }
      },
			xiangshang(id) { //默认选中地区
          this.$get("/common/tree-data/" + id + "?type=" + this.fieldSrcType).then(res => {
            if(id&&id!==0&&id!=='0'){
            if (this.fieldSrc) {
              if(this.fieldSrc.indexOf("|")>-1){
              var srcarr = this.fieldSrc.split("|")
              for (var i in srcarr) {
                var zu = srcarr[i].split("=") //是否有查询条件
                /* if (zu[1].indexOf(",") > -1) { //是否有查询条件内容 -- value 可能有多个
                  var zuzu = zu[1].split(",")
                } */ //没有多个查询条件是 key等于什么
                  if (zu[0] == "types") {
                    if (zu[1] <= res.data.data.level) {
                        this.neirong = {
                          id:res.data.data.id,
                          level:res.data.data.level
                        }
                        this.getdiqu(res.data.data.pid)
                    } else {
                      this.nr.reverse()
                      this.quchu()
                    }
                  } else if (zu[0] == "codes") {
                    if (zu[1] != res.data.data.code) {
                      this.neirong = {
                        id:res.data.data.id,
                        level:res.data.data.level
                      }
                      this.getdiqu(res.data.data.pid)
                    } else {
                      this.nr.reverse()
                      this.quchu()
                    }
                  } else {
                    this.neirong = {
                      id:res.data.data.id,
                      level:res.data.data.level
                    }
                    this.getdiqu(res.data.data.pid)
                  }

              }
            }
            }else{
               this.neirong = {
                id:res.data.data.id,
                level:res.data.data.level
              }
              this.getdiqu(res.data.data.pid)
            }
            }
          })
			},
       quchu(){
        delete this.zhi.share
      },
			qingkong(val) {
        this.nr.splice(val,this.nr.length-val)
        this.span =100/this.nr.length;
			},
			xuan(r) { //选中地区
      /* var index =0;
      for(var i in this.nr){
        for(const z of this.nr[i].value){
          if(r.val == z.id){
           index = i;
          }
        }
      }
      console.log(index) */
      if(r.val){
        this.qingkong(parseInt(r.level)+1)
				this.getdiqu(r.val)
      }else{
         this.qingkong(parseInt(r.level)+1)
      }
      var data = {
        fileNmae:this.fileNmae?this.fileNmae:''
      }
      for(var i in this.nr[this.nr.length - 1].value){
        var obj = this.nr[this.nr.length - 1].value[i]
        if(r.val!==''){
        if(r.val == obj.id){
          data.value = obj
          this.$emit("test", data)
        }
        }else{
            data.value = ''
            this.$emit("test", data)
        }
      }
      if(this.ziduan.jsTrigger){
         if(this.ziduan.jsTime == '1'||this.ziduan.jsTime == '0'){
           eval(this.fileNmae + '('+r.val+')')
        }
      }
				//
			},
			getdiqu(val) { //选择下一级
			  var pid = this.fieldSrc?this.fieldSrc:''
			  if (val) { //是否有传值
			    if (pid.indexOf('pids') > -1) { //是否有pid 得到下一级需要使用到pid  单独判断
			      var arr = pid.split("|")
			      for (var i in arr) { //参数分割 循环
			        if (arr[i].indexOf('pid') > -1) { //分割后值为pid的
			          var prr = arr[i].split("=") //分割pid=1,2
			          prr[1] = val //把1,2换成传入的值
			        }
			        arr[i] = prr.join("=") //吧分割的数组再组装回去
			      }
			      pid = arr.join("|") //吧分割的数组再组装回去
			    } else { //没有传值则需要判断是否需要加| pids必加
			      pid = pid+ (pid ? '|' : '') + 'pids=' + val
			    }
			  } else { //默认拿最顶级地区
          if(pid){
            if(pid.indexOf('pids')==-1){
              pid = pid+'|pids=0'
            }
          }else{
            pid = 'pids=0'
          }

			  }
			  var data = {};
			  data.type = this.fieldSrcType;
			  data.params = pid
			  this.$get("/common/tree-data", data).then(r => {
          if(r.data.data.length){
            if(this.value&&this.zhi.share){
              this.neirong.value = r.data.data;
              this.nr.push(this.neirong)
              if(val&&val!==0&&val!=='0'){
                this.xiangshang(val)
              }else{
                this.nr.reverse()
                this.getdiqu(this.nr[this.nr.length-1].id)
                this.quchu()
              }
              for(var i in this.nr){
                if(!this.nr[i].id){
                  this.nr.splice(i,1)
                }
              }
            }else{
               this.neirong = {
                  value:r.data.data,
                  level:r.data.data[0].level
                }
                this.nr.push(this.neirong)
            }
            this.span = 100/this.nr.length;
          }
			  })
			},
		}
	};
</script>

<style scoped>

</style>
